package com.hotel.config.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 从redis当中读取未被成功推送的消息进入消息队列
 */
@Configuration
public class RedisUnReadWebSocketConfig {
    //普通
    public static final String READ_REDIS_TO_WEBSOCKET_EXCHANGE = "read_redis_to_websocket_exchange";

    public static final String READ_REDIS_TO_WEBSOCKET_QUEUE = "read_redis_to_websocket_queue";

    public static final String READ_REDIS_TO_WEBSOCKET_KEY = "read_redis_to_websocket_key";

    public static final String READ_REDIS_TO_WEBSOCKET_BEAN = "read_redis_to_websocket_bean";
    //死信
    public static final String DEAD_READ_REDIS_TO_WEBSOCKET_EXCHANGE = "dead_read_redis_to_websocket_exchange";

    public static final String DEAD_READ_REDIS_TO_WEBSOCKET_QUEUE = "dead_read_redis_to_websocket_queue";

    public static final String DEAD_READ_REDIS_TO_WEBSOCKET_KEY = "dead_read_redis_to_websocket_key";

    public static final String DEAD_READ_REDIS_TO_WEBSOCKET_BEAN = "dead_read_redis_to_websocket_bean";

    @Bean(READ_REDIS_TO_WEBSOCKET_EXCHANGE)
    public DirectExchange newReadRedisToWebSocketExchange(){
        return new DirectExchange(READ_REDIS_TO_WEBSOCKET_EXCHANGE,true,false);
    }

    @Bean(READ_REDIS_TO_WEBSOCKET_QUEUE)
    public Queue newReadRedisToWebSocketQueue(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",DEAD_READ_REDIS_TO_WEBSOCKET_EXCHANGE);
        map.put("x-dead-letter-routing-key",DEAD_READ_REDIS_TO_WEBSOCKET_KEY);
        return new Queue(READ_REDIS_TO_WEBSOCKET_QUEUE,true,false,false,map);
    }

    @Bean(READ_REDIS_TO_WEBSOCKET_BEAN)
    public Binding newReadRedisToWebSocketBind(){
        return BindingBuilder.bind(newReadRedisToWebSocketQueue()).to(newReadRedisToWebSocketExchange()).with(READ_REDIS_TO_WEBSOCKET_KEY);
    }

    @Bean(DEAD_READ_REDIS_TO_WEBSOCKET_EXCHANGE)
    public DirectExchange newDeadReadRedisToWebSocketExchange(){
        return new DirectExchange(DEAD_READ_REDIS_TO_WEBSOCKET_EXCHANGE,true,false);
    }

    @Bean(DEAD_READ_REDIS_TO_WEBSOCKET_QUEUE)
    public Queue newDeadRedisToWebSocketQueue(){
        return new Queue(DEAD_READ_REDIS_TO_WEBSOCKET_QUEUE,true);
    }

    @Bean(DEAD_READ_REDIS_TO_WEBSOCKET_BEAN)
    public Binding newDeadRedisToWebSocketBind(){
        return BindingBuilder.bind(newDeadRedisToWebSocketQueue()).to(newDeadReadRedisToWebSocketExchange()).with(DEAD_READ_REDIS_TO_WEBSOCKET_KEY);
    }

}
